<?php

namespace addons\helper\controller\wxapp;

use addons\helper\controller\wxapp\Base;
use fast\Tree;

class Index extends Base
{
    protected $noNeedRight = ['index'];

    public function index()
    {   
        // 权限节点列表
        $ruleList = array_values($this->auth->getRuleList($this->userId));
        // 角色组菜单列表
        $menuData = $this->getMenuList($this->userId);
        // 插件配置可见菜单列表
        $configMenuList = get_addon_config('helper')['MenuList'];
        $configMenuList = explode(',',$configMenuList);
        // 扩展菜单
        $extendMenu = [
            [
                'type' => 'extend',
                'name' => 'cache',
                'title' => '清除缓存',
                'icon' => 'fa fa-trash fa-fw',
                'url' => '',
                'childlist' => [
                    [
                        'type' => 'action',
                        'name' => 'all',
                        'title' => '一键清除缓存',
                        'icon' => 'fa fa-trash fa-fw',
                        'url' => 'common/wipecache',
                        'data' => [
                            'type' => 'all',
                        ]
                    ],
                    [
                        'type' => 'action',
                        'name' => 'content',
                        'title' => '清空内容缓存',
                        'icon' => 'fa fa-file-text fa-fw',
                        'url' => 'common/wipecache',
                        'data' => [
                            'type' => 'content',
                        ]
                    ],
                    [
                        'type' => 'action',
                        'name' => 'template',
                        'title' => '清除模版缓存',
                        'icon' => 'fa fa-file-image-o fa-fw',
                        'url' => 'common/wipecache',
                        'data' => [
                            'type' => 'template',
                        ]
                    ],
                    [
                        'type' => 'action',
                        'name' => 'addons',
                        'title' => '清除插件缓存',
                        'icon' => 'fa fa-rocket fa-fw',
                        'url' => 'common/wipecache',
                        'data' => [
                            'type' => 'addons',
                        ]
                    ]
                ]
            ]
        ];
        $menuList = [];
        $menuList['addonMenuList'] = [];
        $menuList['commonMenuList'] = [];
        foreach ($menuData as $k => $v) {
            if ($v['id'] > 84 ) {
                $menuList['addonMenuList'][] = $v;
            }else{
                $menuList['commonMenuList'][] = $v;
            }
        }
        
        $menuList['commonMenuList'] = array_merge($menuList['commonMenuList'],$extendMenu);
        $tabList = [
            0 => [
                'id'   => 'commonMenuList',
                'name' => '常用'
            ],
            1 => [
                'id'   => 'addonMenuList',
                'name' => '插件'
            ]
        ];
        $data = [
            'tabList'        => $tabList,
            'menuList'       => $menuList,
            'configMenuList' => $configMenuList,
            'ruleList'       => $ruleList
        ];
        $this->success('ok', $data);
    }
    /**
     * 获取用户菜单数据
     *
     * @param int $userId 管理员ID
     * @return array
     */
    protected function getMenuList($userId)
    {
        // 读取管理员当前拥有的权限节点
        $userRule = $this->auth->getRuleList($userId);

        $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');

        $ruleList = collection(\app\admin\model\AuthRule::where('status', 'normal')
            ->where('ismenu', 1)
            ->order('weigh', 'desc')
            ->cache("__menu__")
            ->select())->toArray();

        $indexRuleList = \app\admin\model\AuthRule::where('status', 'normal')
            ->where('ismenu', 0)
            ->where('name', 'like', '%/index')
            ->column('name,pid');

        $pidArr = array_filter(array_unique(array_map(function ($item) {
            return $item['pid'];
        }, $ruleList)));

        foreach ($ruleList as $k => &$v) {
            if (!in_array($v['name'], $userRule)) {
                unset($ruleList[$k]);
                continue;
            }
            $indexRuleName = $v['name'] . '/index';
            if (isset($indexRuleList[$indexRuleName]) && !in_array($indexRuleName, $userRule)) {
                unset($ruleList[$k]);
                continue;
            }
            $v['icon']   = $v['icon'] . ' fa-fw';
            $v['py']     = $pinyin->abbr($v['title'], '');
            $v['pinyin'] = $pinyin->permalink($v['title'], '');
            $v['name']   = rtrim(ltrim($v['name'], '/'), '/');
            $v['url']    = rtrim(ltrim($v['name'], '/'), '/');
            // 去掉菜单名称中的'管理'字符
            $v['title'] = mb_substr($v['title'], mb_strlen($v['title'], 'utf-8') - 2, 2, 'utf-8') === '管理' ? mb_substr($v['title'], 0, mb_strlen($v['title'], 'utf-8') - 2, 'utf-8') : $v['title'];
        }
        $lastArr = array_diff($pidArr, array_filter(array_unique(array_map(function ($item) {
            return $item['pid'];
        }, $ruleList))));
        foreach ($ruleList as $index => $item) {
            if (in_array($item['id'], $lastArr) || $item['id'] === 1) { //去掉'控制台'菜单
                unset($ruleList[$index]);
            }
            $item['title'] = __($item['title']);
        }
        // 构造菜单数据
        Tree::instance()->init($ruleList);
        $menu = Tree::instance()->getTreeArray(0, '');
        return $menu;
    }
}
